package com.ash.servlet;

import com.ash.entity.Constant;
import com.ash.entity.User;
import com.ash.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2020/12/25 17:48
 * @file : test06.${PACKAGE_NAME}.${NAME}.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("欢迎来到loginServlet");
        String userName = req.getParameter("userCode");
        String password = req.getParameter("userPassword");
        User user = new User();
        user.setUserCode(userName);
        user.setUserPassword(password);
        UserServiceImpl userService = new UserServiceImpl();
        User login = userService.login(user);
        if(login==null){
            req.setAttribute("error","用户名或者密码不正确！");
            req.getRequestDispatcher("login.jsp").forward(req,res);
        }
        req.getSession().setAttribute(Constant.USER_SESSION,login);
        /*不加斜杠它会自动帮你加上项目路径*/
        res.sendRedirect("jsp/frame.jsp");

    }
}